package com.panshi.modules.controller;

import com.panshi.api.AnswerApi;
import com.panshi.po.*;
import com.panshi.vo.*;
import com.panshi.modules.domain.QaAnswerUncheck;
import com.panshi.modules.service.QaAnswerCommentService;
import com.panshi.modules.service.QaAnswerService;
import com.panshi.modules.service.QaAnswerUncheckService;
import com.panshi.modules.service.ThRaiseDownService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("answer-api")
@Slf4j
@CrossOrigin
public class AnswerController implements AnswerApi {

    @Autowired
    QaAnswerUncheckService qaAnswerUncheckService;
    @Autowired
    QaAnswerService qaAnswerService;
    @Autowired
    QaAnswerCommentService qaAnswerCommentService;
    @Autowired
    ThRaiseDownService thRaiseDownService;

    /*回答审核增加*/
    public Result save(SaveAnswerVO saveAnswerVO) {
        Result result = new Result();
        QaAnswerUncheck qaAnswerUncheck = qaAnswerUncheckService.insert(saveAnswerVO);
        if (!StringUtils.isEmpty(qaAnswerUncheck)) {
            return result.ok();
        }
        return result.error();
    }

   /* 回答查询(审核表)*/
    public Result<List<AnswerAudit>> queryAnswer(QueryAnswerAuditVO queryAnswerAuditVO) {
        Result<List<AnswerAudit>> result = new Result();
        List<AnswerAudit> answerAudits = qaAnswerUncheckService.queryAnswer(queryAnswerAuditVO);
        if (answerAudits.size()==0) {
            return result.error();
        }
        result.setData(answerAudits);
        return result;
    }

    /* 回答查询总数(审核表)*/
    public Result<Integer> queryAuditCount(QueryAnswerAuditVO queryAnswerAuditVO) {
        Integer count = qaAnswerUncheckService.queryAuditCount(queryAnswerAuditVO);
        Result<Integer> result = new Result<>();
        result.ok().setData(count);
        return result;
    }

    @Override
    public Result updChoose(final UpdAnswerChooseVO updAnswerChooseVO) {
        int i = qaAnswerService.updChoose(updAnswerChooseVO);
        if (i > 0) {
            return new Result().ok();
        }
        return new Result().error();
    }

    /*回答审核删除*/
    public Result delUncheck(Long id) {
        Result result = new Result<>();
        int i = qaAnswerUncheckService.del(id);
        if (i > 0) {
            return result.ok();
        }
        return result.error();
    }

    /*修改回答审核内容*/
    public Result update(UpdAnswerUncheckedVO updAnswerUncheckedVO) {
        boolean b = qaAnswerUncheckService.updateById(updAnswerUncheckedVO);
        Result result = new Result<>();
        if (b) {
            return result.ok();
        }
        return result.error();
    }

    /*修改回答审核状态*/
    public Result updStatus(UpdAnswerUncheckedStatusVO updAnswerUncheckedStatusVO) {
        boolean b = qaAnswerUncheckService.updStatus(updAnswerUncheckedStatusVO);
        if (b) {
            return new Result<>().ok();
        }
        return new Result<>().error();
    }

    /*删除*/
    public Result del(Long id) {
        int i = qaAnswerService.del(id);
        if (i > 0) {
            return new Result<>().ok();
        }
        return new Result<>().error();
    }

    /*查询(通过表)*/
    public Result<List<Answer>> selectPage(QueryAnswerVO queryAnswerVO) {
        List<Answer> answers = qaAnswerService.selectPage(queryAnswerVO);
        Result<List<Answer>> result = new Result<>();
        if (answers.size()==0) {
            return result.error();
        }
        result.ok().setData(answers);
        return result;
    }


    /*查询总数(通过表)*/
    public Result<Integer> queryCount(QueryAnswerVO queryAnswerVO) {
        Integer count = qaAnswerService.queryCount(queryAnswerVO);
        Result<Integer> result = new Result<>();
        result.ok().setData(count);
        return result;
    }

    /*增加回答评论*/
    public Result saveComment(final SaveCommentVO saveCommentVO) {
        int i = qaAnswerCommentService.insert(saveCommentVO);
        if (i > 0) {
            return new Result<>().ok();
        }
        return new Result<>().error();
    }

    /*查询子回复*/
    public Result<List<RevertAll>> revertAll(final QueryCommentVO queryCommentVO) {
        List<RevertAll> revertAlls = qaAnswerCommentService.revertAll(queryCommentVO);
        Result<List<RevertAll>> result = new Result<>();
        result.ok().setData(revertAlls);
        return result;
    }

    /*回答&回答评论 点踩*/
    public Result<Option> option(@RequestBody SaveOptionVO saveOptionVO) {
        int i = thRaiseDownService.optionVO(saveOptionVO);
        Result<Option> result = new Result<>();
        Option option = new Option();
        option.setOptionType(saveOptionVO.getPattern());
        if (i > 0) {
            option.setYes(true);
            result.ok().setData(option);
            return result;
        }else if(i == -1){
            option.setYes(false);
            result.ok().setData(option);
            return result;
        }
        return result.error();
    }

    /*查询回复*/
    public Result<List<CommentList>> commentlist(final QueryCommentVO queryCommentVO) {
        List<CommentList> list = qaAnswerCommentService.commentlist(queryCommentVO);
        Result<List<CommentList>> result = new Result<>();
        result.ok().setData(list);
        return result;
    }

   /*查询回复总数*/
    public Result<Integer> commentCount(final QueryCommentVO queryCommentVO) {
        Integer count = qaAnswerCommentService.commentCount(queryCommentVO);
        Result<Integer> result = new Result<>();
        result.setData(count);
        return result;
    }


    /**
     * 页面传入日期报错问题处理
     *
     * @param binder
     */
    @InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }

}